package com.htd.erp.web;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.SubnodeConfiguration;
import org.apache.commons.configuration.XMLConfiguration;

@Named
@ApplicationScoped
public class MenuFactory implements Serializable {

	private static final long serialVersionUID = -221833288552874827L;
	
	// @Inject private User loginUser;
	
	@Produces
	@Named("rootMenu")
	@SessionScoped
	public Menu loadMenu() throws ConfigurationException {
		return createMenu(new XMLConfiguration("menu.xml"));
	}
	
	@SuppressWarnings("unchecked")
	protected Menu createMenu(HierarchicalConfiguration configuration) {
		Menu menu = new Menu();
		menu.setLabelKey(configuration.getString("[@labelKey]"));
		menu.setHref(configuration.getString("[@href]"));
		List<SubnodeConfiguration> childConfigurations = configuration.configurationsAt("menu");
		for(SubnodeConfiguration childConfiguration : childConfigurations) {
			menu.addSubMenu(createMenu(childConfiguration));
		}
		return menu;
	}
}
